<template>
  <div class="managers">
    <div class="top flex-between">
      <el-button type="primary" size="medium" @click="visible=true">+ 新增管理员</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="medium"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="group" label="管理员等级"></el-table-column>
        <el-table-column align="center" prop="username" label="账号"></el-table-column>
        <el-table-column align="center" prop="reg_time" label="创建时间"></el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="item">
            <el-button
              type="text"
              @click="edit(item.row.uid,2,item.$index)"
              :loading="onChange===item.$index"
              v-if="item.row.status==1"
            >停用</el-button>
            <el-button
              type="text"
              @click="edit(item.row.uid,1,item.$index)"
              :loading="onChange===item.$index"
              v-if="item.row.status==2"
            >恢复</el-button>
            <el-button type="text" style="color:red" @click="del(item.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages :pages="searchForm" :total="total" @pageChange="pageChange" layout="prev, pager, next"></pages>
      <!-- 增加窗口 -->
      <add-manager
        :platform_id="searchForm.platform_id"
        :visible="visible"
        @success="visible=false;search()"
        @cancel="visible=false"
      ></add-manager>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import AddManager from "./AddManager";
import { managerList, delManager, editManager } from "./system.service.js";
export default {
  components: {
    Pages,
    AddManager
  },
  data() {
    return {
      loading: false,
      onChange: "",
      tableData: [],
      visible: false,
      total: 0,
      searchForm: {
        page: 1,
        size: 10,
        platform_id: this.$store.state.userInfo.platform_id
      }
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    async search() {
      this.loading = true;
      const res = (await managerList(this.searchForm)) || [];
      this.tableData = res.data.items || [];
      this.total = res.data.total || 0;
      this.loading = false;
    },
    del(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${
          item.username
        }</span> 管理员?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true
        }
      )
        .then(() => {
          delManager({ uid: item.uid }).then(res => {
            if (res.code === 0)
              this.$notify.success({
                title: "提示",
                message: "删除成功",
                duration: 2000
              });
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async edit(uid, status, index) {
      this.onChange = index;
      await editManager({ uid, status });
      this.onChange = "";
      this.search();
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss">
.managers {
  position: relative;
  min-height: calc(100vh - 100px);
  .top {
    .input-with-select {
      width: 341px;
      .el-input-group__append {
        background-color: #66b1ff;
        color: #fff;
      }
    }
  }
  .data-table {
    position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 20px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .scaleImg {
      max-width: 120px;
      max-height: 23px;
      cursor: pointer;
    }
    .link {
      color: #409eff !important;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>